package com.xiaotu.code.system;

import java.io.Serializable;
import java.util.List;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.util.Assert;

import com.xiaotu.code.model.quene.SettingMqQuene;
import com.xiaotu.code.service.mquene.SettingMqQueneService;

/**
 * 队列消息发送者基类.
 * 
 * @author chenxiangrong
 * 
 */
public abstract class AbstractQueneProducer implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 4412249612617564171L;
	protected final Logger logger = LoggerFactory.getLogger(getClass());

	@Resource
	private JmsTemplate jmsTemplate;

	@Autowired
	private SettingMqQueneService settingMqQueneService;

	public abstract String getQueueName();

	public void sendMessage(BaseQueneMessage message) throws ServiceException {

		Assert.notNull(this.getQueueName());

		this.beforeSend(message);

		SettingMqQuene settingMqQuene = settingMqQueneService.getLitenerByQueneName(getQueueName());
		if (settingMqQuene == null) {
			throw new ServiceException("未配置或者未启用SettingMqQuene：queneName=" + this.getQueueName());
		}
		// 查找监听者，并分别发送.
		String strLisNodes = settingMqQuene.getListenNodes();
		String[] strNodes = strLisNodes.split(",");
		for (String strNode : strNodes) {
			String curNodeId = "0";
			if (!settingMqQuene.getEnabledLocal() && strNode.equals(curNodeId)) {
				continue;
			}
			String queue = QueneSubscriberContainer.generalQueneName(message.getFromNodeId(), this.getQueueName(),
					Integer.valueOf(strNode));
			this.jmsTemplate.convertAndSend(queue, message);
		}
		this.afterSend(message);
	}

	/**
	 * 发送数据到指定节点.
	 * 
	 * @param message
	 * @param nodeId
	 * @throws ServiceException
	 */
	public void sendMessage(BaseQueneMessage message, Integer nodeId) throws ServiceException {

		Assert.notNull(this.getQueueName());
		Assert.notNull(nodeId);
		this.beforeSend(message);

		String queue = QueneSubscriberContainer.generalQueneName(message.getFromNodeId(), this.getQueueName(), nodeId);
		this.jmsTemplate.convertAndSend(queue, message);
		this.afterSend(message);
	}

	/**
	 * 发送数据到多个指定节点.
	 * 
	 * @param message
	 * @param nodeIds
	 * @throws ServiceException
	 */
	public void sendMessage(BaseQueneMessage message, List<Integer> nodeIds) throws ServiceException {

		Assert.notNull(this.getQueueName());
		Assert.notNull(nodeIds);
		this.beforeSend(message);
		for (Integer nodeId : nodeIds) {
			String queue = QueneSubscriberContainer.generalQueneName(message.getFromNodeId(), this.getQueueName(),
					nodeId);
			this.jmsTemplate.convertAndSend(queue, message);
		}
		this.afterSend(message);
	}

	public boolean beforeSend(BaseQueneMessage message) throws ServiceException {
		Assert.notNull(message);
		return true;
	}

	public void afterSend(@SuppressWarnings("unused") BaseQueneMessage message) throws ServiceException {
		logger.debug("发送同步消息成功.{}", getQueueName());
	}

	public JmsTemplate getJmsTemplate() {
		return jmsTemplate;
	}
}
